package com.example.schoolexam2.service.impl;

import com.example.schoolexam2.entity.DepartmentTableBean;
import com.example.schoolexam2.entity.Employee;
import com.example.schoolexam2.entity.PageVO;
import com.example.schoolexam2.entity.PowerVO;
import com.example.schoolexam2.mapper.PowerMapper;
import com.example.schoolexam2.service.PowerService;
import com.alibaba.fastjson.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class PowerServiceImpl implements PowerService {
    @Autowired
    PowerMapper powermapper;

    @Override
    public JSONArray getPower(PowerVO power) {
        List<String> typeList = powermapper.getPowerType(power);
        List<PowerVO> powerlist = powermapper.getPower(power);
        JSONArray result = new JSONArray();
        for (String type:typeList) {
            JSONObject module = new JSONObject();
            module.put("cardname",type);
            module.put("cardtype",type);
            JSONArray data = new JSONArray();
            JSONArray l = new JSONArray();
            for (PowerVO vo:powerlist) {
                if(type.equals(vo.getMname())){
                    JSONObject o = new JSONObject();
                    o.put("cardicon",vo.getIcon());
                    o.put("cardtitle",vo.getPname());
                    o.put("cardurl",vo.getURL());
                    l.add(o);
                }
            }
            data.add(l);
            module.put("data",data);
            result.add(module);
        }
        return result;
    }

    @Override
    public JSONArray getTearcherBydepartment() {
        List<DepartmentTableBean> deptlist = powermapper.getDepartment();
        List<Employee> teacherlist = powermapper.getTearcher();
        JSONArray result = new JSONArray();
        for (DepartmentTableBean dept:deptlist) {
            JSONObject d = new JSONObject();
            d.put("id",dept.getDepartmentId());
            d.put("label",dept.getDepartmentName());
            JSONArray children = new JSONArray();
            for (Employee teacher:teacherlist){
                if(null!=teacher.getDepartment()&&teacher.getDepartment().equals(dept.getDepartmentId())){
                    JSONObject o = new JSONObject();
                    o.put("id",teacher.getEmployeeId());
                    o.put("label",teacher.getEmployeeName());
                    children.add(o);
                }
            }
            d.put("children",children);
            if(children.size()>0){
                result.add(d);
            }
        }
        return result;
    }

    @Override
    public JSONArray getPages() {
        List<PageVO> pagelist = powermapper.getPages();
        JSONArray result = new JSONArray();
        for (PageVO page:pagelist) {
            JSONObject object = new JSONObject();
            object.put("label",page.getPname());
            object.put("key",page.getPid());
            result.add(object);
        }
        return result;
    }

    @Override
    public void updatePowerById(JSONObject object) {
        JSONArray userlist = (JSONArray) object.get("user");
        JSONArray pagelist = (JSONArray) object.get("pages");
        for (int i=0;i<userlist.size();i++) {
            JSONObject user = userlist.getJSONObject(i);
            String utype = "teacher";
            int pageid = 0;
            for (int j=0;j<pagelist.size();j++) {
                pageid = Integer.valueOf(pagelist.getInteger(j));
                String temp = user.get("id").toString();
                powermapper.addPower(user.get("id").toString(),utype,pageid);
            }
        }
    }

    @Override
    public List<DepartmentTableBean> getDepartInfo() {
        return powermapper.getDepartment();
    }
}
